<?php
/**
*售后总监
*/
require("../include/init.php");

$mysql=mysql::getIns();

date_default_timezone_set('Asia/Shanghai');









$sql="select areaname from area";
$area=$mysql->getAll($sql);
foreach($area as $v){
  $address=$v['areaname'];
  $maxtime=time();
  $mintime=$maxtime-10*60;
  // echo $maxtime . '<br />';
  // echo $mintime;
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='". $address ."'";
  $allnow=$mysql->getOne($sql);
  // echo $allnow;
  // print_r($allnow);
  // exit;


  //统计当天累计人数数
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
  $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop='" . $address . "'";
  $allday=$mysql->getOne($sql);

  //首次今天首次进店人数,通过字段visittimes为1来进行判断
  $sql="select count(*) from alldev where whitelist <> '1' and visittimes=1 and timestamp > $mintime and timestamp < $maxtime and shop='".$address ."'";
  $firstlogin=$mysql->getOne($sql);

  //统计当前销售区
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."' and area in ('展厅区','销售区','精品区')";
  $sale=$mysql->getOne($sql);
  // echo $sale;
  // print_r($sale);
  // exit;

  //统计当前售后区
  $service=$allnow-$sale;


  //今天多次进店人数
  $sql="select count(*) from alldev where whitelist <> '1' and visittimes <> 1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $second=$mysql->getOne($sql);

  //统计登陆人数
  $sql="select count(*) from alldev where whitelist <> '1' and isOL=1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $login=$mysql->getOne($sql);
  // echo $login;

  //统计未登陆人数
  $sql="select count(*) from alldev where whitelist <> '1' and isOL=2 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $nolog=$mysql->getOne($sql);
  // echo $nolog;

  //统计历史进店人数
  $sql="select count(*) from alldev where whitelist <> '1' and shop='". $address ."'";
  $history=$mysql->getOne($sql);


  /*******************************************************

  进店量


  *****************************************************/

  //今天的进店量即为上述计算的今日连接数$allday

  //平均值也为此$allday

  /***********
  *获取上周时间
  *
  **********/
  $date=date('Y-m-d');  //当前日期
  $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  $now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
  $now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
  $last_start=date('Y-m-d',strtotime("$now_start - 7 days"));  //上周开始日期
  $last_end=date('Y-m-d',strtotime("$now_start - 1 days"));  //上周结束日期
  $one=strtotime($last_start);
  $two=$one+24*3600*1;
  $three=$one+24*3600*2;
  $four=$one+24*3600*3;
  $five=$one+24*3600*4;
  $six=$one+24*3600*5;
  $seven=$one+24*3600*6;
  $nowone=$one+24*3600*7;

  //本周的值
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $one and timestamp < $nowone and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='" . $address . "'";
  $allweek=$mysql->getOne($sql);


  //获取这个个月第一天时间
  $first =mktime(0,0,0,date('m',time()),1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$first);
  // exit;


  //获取这个月第五周时间
  $fifth =mktime(0,0,0,date('m',time())+1,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$fifth);
  // exit;

  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $first and timestamp < $fifth and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='" . $address . "'";
  $allmonth=$mysql->getOne($sql);


  //今年第一天和最后一天时间
  $year=date("Y",time());
  $firsttime=$year."-01-01";
  // echo $first;
  $firsttime=strtotime($firsttime);
  // echo date('Y-m-d H:i:s',$first);
  // exit;
  $years=$year+1;
  $endtime=$years."-01-01";
  $endtime=strtotime($endtime);
  // echo date('Y-m-d H:i:s',$end);
  // exit;

  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $firsttime and timestamp < $endtime and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='" . $address . "'";
  $allyear=$mysql->getOne($sql);






  /****************************************


  		4s店的wifi登录率


  ****************************************/


  //总人数为今天累计人数
  $maxtime=time();
  $mintime=$maxtime-10*60;



  //统计当前在线人数    (在线人数为当前时间的十分钟之前到现在的人数)


  $maxtime=time();
  $mintime=$maxtime-10*60;
  // echo $maxtime . '<br />';
  // echo $mintime;
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='". $address ."'";
  $allnow=$mysql->getOne($sql);


  //统计登陆人数
  $sql="select count(*) from alldev where whitelist <> '1' and isOL=1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $denglu=$mysql->getOne($sql);
  // echo $login;

  // echo $allday;
  // exit;
  // echo $denglu;
  $loginnum=ceil(($denglu/$allnow)*100);
  // echo $loginnum;
  // exit;

  //未登录
  $nologinnum=100-$loginnum;




  /********************************************************************

  全国集客对比


  ***********************************************************************/

  //本店值月上述的一样

  //全国今天的值
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop <> 'Audi City BeiJing'";
  $alldaycountry=$mysql->getOne($sql);
  // echo $alldaycountry;
  // exit;
  $sql="select shop from alldev group by shop";
  $num=$mysql->getAll($sql);
  // print_r($num);
  $num=count($num)-1;
  // echo $num;
  // exit;
  //全国今天的平均值
  $daycountry=ceil($alldaycountry/$num);
  // echo $daycountry;
  // exit;
  //全国本周的值

  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $one and timestamp < $end and shop <> 'Audi City BeiJing'";
  $allweekcountry=$mysql->getOne($sql);
  //全国本周的平均值
  $weekcountry=ceil($allweekcountry/$num);


  //全国本月的平均值
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $first and timestamp < $fifth and shop <> 'Audi City BeiJing'";
  $allmonthcountry=$mysql->getOne($sql);
  $monthcountry=ceil($allmonthcountry/$num);


  //全国本年的值
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $firsttime and timestamp < $endtime and shop <> 'Audi City BeiJing'";
  $allyearscountry=$mysql->getOne($sql);
  $yearcountry=ceil($allyearscountry/$num);
  //写入redis
  // echo $address;
  switch ($address) {
    case '义乌市博奥汽车销售服务有限公司':
      $hash='4S_ServiceDirector_main_ywba';
      break;
    case '广东粤奥汽车销售服务有限公司':
      $hash='4S_ServiceDirector_main_gdya';
      break;
    case '辽宁鑫迪汽车销售服务有限公司':
      $hash='4S_ServiceDirector_main_lnxd';
      break;
    case '江苏天奥汽车销售服务有限公司':
      $hash='4S_ServiceDirector_main_jsta';
      break;
    case '成都新元素雅麓汽车销售服务有限公司':
      $hash='4S_ServicetDirector_main_cdyl';
      break;
    case '云南驰泰汽车销售服务有限公司':
      $hash='4S_ServiceDirector_main_ynct';
      break;
    case '宁夏奥立升汽车销售服务有限公司':
      $hash='4S_ServiceDirector_main_nxals';
      break;
    case '陕西奥诚汽车销售服务有限公司':
      $hash='4S_ServiceDirector_main_sxac';
      break;
    case '北京博瑞祥星汽车销售有限公司':
      $hash='4S_ServiceDirector_main_brxx';
      break;
  }
// echo $hash;
$redis = new Redis();
//连接服务器
$redis->connect("localhost");
//授权
$redis->auth("redis=pwd@2016");

  $redis->hmset($hash,
  array(
    'allnow'=>$allnow,
    'allday'=>$allday,
    'firstlogin'=>$firstlogin,
    'sale'=>$sale,
    'service'=>$service,
    'secondlogin'=>$second,
    'login'=>$login,
    'nolog'=>  $nolog,
    'history'=>$history,
    'allweek'=>$allweek,
    'allmonth'=>$allmonth,
    'allyear'=>$allyear,
    'denglu'=>$denglu,
    'loginnum'=>$loginnum,
    'nologinnum'=>$nologinnum,
    'alldaycountry'=>$alldaycountry,
    'daycountry'=>$daycountry,
    'allweekcountry'=>$allweekcountry,
    'weekcountry'=>$weekcountry,
    'monthcountry'=>$monthcountry,
    'allyearscountry'=>$allyearscountry,
    'yearcountry'=>$yearcountry,
  ));

}
